#include "GenericTypeDefs.h"
#include "nRF2401.h"
#include "delay.h"
#include "CommandProc.h"

unsigned  char DATA_BUF;

void RF_config(unsigned long int rec_addr, unsigned char power, unsigned char channal)
{
	nRF2401_Conf[14]=rec_addr&0x000000ff;
	nRF2401_Conf[13]=(rec_addr>>8)&0x000000ff;
	nRF2401_Conf[12]=(rec_addr>>16)&0x000000ff;
	nRF2401_Conf[11]=(rec_addr>>24)&0x000000ff;
	nRF2401_Conf[10]=0x00000000;
	nRF2401_Conf[16]=0x03&power;
	nRF2401_Conf[17]&=0xfe;
	nRF2401_Conf[17]|=(channal<<1);
}

#ifndef __TIMER_DELAY
void Delay100(void)
{ 
	unsigned int i; 
	for(i=0;i<100;i++);
}

void delay_100us(void)
{
	unsigned char p;
	for(p = 2;p > 0;p--)
	{
		Delay100();
	}
}

void delay_1ms()
{
	unsigned int t = 4;
	do
	{
		Delay100();
		t--;
	}
	while(t);
}

void delay_ms(unsigned int temp_time)
{
    unsigned int index;
    for (index = 0; index < temp_time; index ++)
    {
        delay_1ms();
    }
}
#endif //__TIMER_DELAY

unsigned char ByteRead(void)
{
	unsigned char i = 0;
	for (i=0; i<8; i++)
	{
         DATA_BUF = DATA_BUF << 1;
		SET_CLK();
	//	SET_DATA();	
         //for(k=100;k>0;k--);
		if (GET_DATA())	//��ȡ���λ����������ĩβ��ͨ������λ�������ֽ�
		{
			DATA_BUF |= BYTE_BIT0;
		}
		else
		{
			DATA_BUF &= ~BYTE_BIT0;
		}
     
		CLEAR_CLK();

     }
	 return DATA_BUF;
}


void ByteWrite(unsigned char send)
{
	unsigned char i;

	DATA_BUF = send;
	//CLEAR_CLK();
	for (i=0; i<8; i++)
	{
		if (DATA7)	//���Ƿ������λ
		{
			SET_DATA();
		}
		else
		{
			CLEAR_DATA();
		}
		SET_CLK();
		DATA_BUF = DATA_BUF << 1;
		CLEAR_CLK();
	}
}


void Config2401(void)
{ 
	unsigned int i = 0; 
	unsigned char variablel;
	//RF2401�������÷�ʽ
	CLEAR_PWR();
	CLEAR_CS();
	CLEAR_CE();
	CLEAR_CLK();
	CLEAR_DATA();
	delay_1ms();delay_1ms();delay_1ms();delay_1ms();delay_1ms();

	SET_PWR();
	delay_1ms();
	delay_1ms();    //��ʱ3ms
	delay_1ms();
	delay_1ms();delay_1ms();delay_1ms();delay_1ms();delay_1ms();

        
	SET_CS();
	//PWR_DWN -> Configuration_mode	Delay_3ms   ����3��ĵ����ʱ
	for(i=0; i<18; i++)
	{ 
		variablel = nRF2401_Conf[i]; 
		ByteWrite(variablel);
	} 

	Delay100();	// configuration mode -> stand by mode ??
	CLEAR_CS();     //CS�õ�ʹ������Ч
        CLEAR_PWR();
	Delay100(); 
} 


void nRF2401_SetTxMode(void)
{ 
	//����Ϊ����ģʽ
        SET_PWR();
	delay_1ms();
	delay_1ms();    //��ʱ3ms
	delay_1ms();
	delay_1ms();delay_1ms();delay_1ms();delay_1ms();delay_1ms();

	CLEAR_CE();
	SET_CS();
	delay_100us(); 	//���üĴ���0�ֽ�RXEN ����Ϊ0:����ģʽ
	CLEAR_DATA();
	SET_CLK();
	CLEAR_CLK();		//����ΪActivemodes(Tx)
	
	CLEAR_CS();	

	SET_CE();
	delay_100us();

}


void nRF2401_SetRxMode(void)
{ 
        SET_PWR();
	delay_1ms();
	delay_1ms();    //��ʱ3ms
	delay_1ms();
	delay_1ms();delay_1ms();delay_1ms();delay_1ms();delay_1ms();

	//delay_100us(); 	//����Ϊ����ģʽ
	CLEAR_CE();
	SET_CS();
	Delay100();		//���üĴ���0�ֽ�RXEN ����Ϊ1:����ģʽ
	SET_DATA(); 
	SET_CLK();
	CLEAR_CLK();		//����ΪActivemodes(Rx)

	CLEAR_CS();
	SET_CE();
	Delay100();
}


//=========================================================================================================
//�������:RF2401_TxPacket
//������:��byte����ݰ��̶���ʽ������2401
//��	��:*TxBuf(byte)	���͵����
//��	��:��
//=========================================================================================================
unsigned char TxAddress[ADDR_W/8]={0xcc,0xcc,0xcc,0xcc};


void nRF2401_set_tx_addr(unsigned long int addr)	//���÷��͵�ַ
{
	TxAddress[0]=addr>>24;
	TxAddress[1]=((addr<<8)>>24);
	TxAddress[2]=(addr<<16)>>24;
	TxAddress[3]=(addr<<24)>>24;	
}

void nRF2401_TxPacket(unsigned char TxBuf[])
{
	int i; 
	unsigned char variable2; 
	
	
	SET_CE();
	Delay100(); 
	for(i=0;i< (ADDR_W/8);i++)	//д����յ�ַ(���ֽڶ���)
	{
		variable2=TxAddress[i]; 
		ByteWrite(variable2);
	} 

	for(i=0;i<(DATA1_W/8);i++)	//д����Ҫ���͵����(���ֽڶ���)
	{
		variable2=TxBuf[i]; 
		ByteWrite(variable2);
	} 
	CLEAR_CE(); //CE�õ�ʹ������Ч
	Delay100(); 
	Delay100(); 
}




unsigned char nRF2401_RxPacket(unsigned char *RxBuf)
{ 
	

	unsigned int i;

	if(GET_DR())
	{
		TRISBbits.TRISB9=1;
		for (i=0; i<DATA1_W/8; i++)
		{
			RxBuf[i]	=ByteRead();		//��DATA�Ż���������
		}
		TRISBbits.TRISB9=0;
         return 0x01;
	}
	return 0X00;
}





void nRF2401_port_init()
{
    TRISBbits.TRISB15=0;	//PWR
    TRISBbits.TRISB13=0;	//CE
    TRISBbits.TRISB12=0;	//CS
    TRISBbits.TRISB10=0;	//CK1
    TRISBbits.TRISB9=0;	//DAT1
    TRISBbits.TRISB11=1;	//DR1
	
	
	TRISBbits.TRISB14=0; //��
	TRISAbits.TRISA1=1; //����
	TRISAbits.TRISA1=1; //����
	CLEAR_DATA();
	CLEAR_CLK();
	
	delay_1ms(); 	delay_1ms(); 	delay_1ms(); 
   	Config2401();
   	delay_1ms(); 	delay_1ms(); 	delay_1ms(); 
   // 	SetRxMode();
    //	delay_1ms(); 
}

